package versionControl.aspectj.library;

import java.io.IOException;
import java.util.Date;
import com.ajexperience.utils.DeepCopyException;
import com.ajexperience.utils.DeepCopyUtil;

public aspect CreateNew {
	/*
	 * Upon creation of a new versioned object - create and store a new 
	 * version of the object 
	 */
	pointcut versionedConstructorExecution(Versioned versionedObject):
    	target(versionedObject) && execution(public Versioned+.new(..)) && !within(versionControl.aspectj.library.*);
	
	after(Versioned object) : versionedConstructorExecution(object)
	{	
		try{
			DeepCopyUtil deepCopyUtil = new DeepCopyUtil();
			Versioned versionedObject = deepCopyUtil.deepSerializedCopy(object);
			versionedObject.setComment(new String("object creation"));
			
			versionedObject.setVersionID(0);    	
	    	versionedObject.setTimeStamp(new Date());
	    	    	
	    	object.addVersion(versionedObject);  
		}
		catch(DeepCopyException e){
			e.printStackTrace();
		}
		catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}	
}
